package com.lu.adog.util.image;

import android.graphics.Bitmap;

/**
 * 图片拆分与合并
 * Created by Yue on 2017/11/19.
 */
public class SplitMergeRGB {

    private static final int MODE_RED = 1;
    private static final int MODE_GREEN = 2;
    private static final int MODE_BLUE = 3;


    /**
     * 获取red通道位图
     * @param image
     * @return
     */
    public static Bitmap getRedGray(Bitmap image){
        return splitRGB(image,MODE_RED);
    }

    /**
     * 获取green通道位图
     * @param image
     * @return
     */
    public static Bitmap getGreenGray(Bitmap image){
        return splitRGB(image,MODE_RED);

    }

    /**
     * 获取blue通道位图
     * @param image
     * @return
     */
    public static Bitmap getBlueGray(Bitmap image){
        return splitRGB(image,MODE_RED);
    }

    private static Bitmap splitRGB(Bitmap image,int mode) {
        int width = image.getWidth();   //获取位图的宽
        int height = image.getHeight();  //获取位图的高

        Bitmap result = Bitmap.createBitmap(width, height, image.getConfig());

        int alpha = 0xff;
        int r = 0;
        int g = 0;
        int b = 0;

        int gray = 0;

        for (int i = 0; i < width; i++) {//一行行扫描
            for (int j = 0; j < height; j++) {

                gray = image.getPixel(i, j);

                alpha = gray >>> 24;
                r = (gray >> 16) & 0x000000ff;
                g = (gray >> 8) & 0x000000ff;
                b = gray & 0x000000ff;

                switch (mode) {
                    case MODE_RED:
                        gray = (int) (r * 0.299f + g * 0.587f + b * 0.114f);
                        break;
                    case MODE_GREEN:
                        gray = g;
                        break;
                    case MODE_BLUE:
                        gray = b;
                        break;
                    default:
                        gray = (int) (r * 0.299f + g * 0.587f + b * 0.114f);
                        break;
                }
                gray = (alpha << 24) | (gray << 16) | (gray << 8) | gray;

                result.setPixel(i, j, gray);
            }

        }
        return result;
    }

    private static Bitmap[] splitRGB(Bitmap image){
        int width = image.getWidth();   //获取位图的宽
        int height = image.getHeight();  //获取位图的高

        Bitmap resultR = Bitmap.createBitmap(width, height, image.getConfig());
        Bitmap resultG = Bitmap.createBitmap(width, height, image.getConfig());
        Bitmap resultB = Bitmap.createBitmap(width, height, image.getConfig());

        int alpha = 0;
        int r = 0;
        int g = 0;
        int b = 0;

        int color = 0;

        for (int i = 0; i < width; i++) {//一行行扫描
            for (int j = 0; j < height; j++) {

                color = image.getPixel(i, j);

                alpha = color >>> 24;
                r = (color >> 16) & 0x000000ff;
                g = (color >> 8) & 0x000000ff;
                b = color & 0x000000ff;

                int alpha24 = alpha<<24;
                resultR.setPixel(i, j, (alpha24 | (r<<16)));
                resultG.setPixel(i,j,(alpha24 | (g<<8)));
                resultB.setPixel(i,j,(alpha24 | b));
            }

        }
        return new Bitmap[]{resultR,resultG,resultB};

    }
}
